package cn.edu.sdjzu.cs.dingcan.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.sdjzu.cs.dingcan.entity.Customer;
import cn.edu.sdjzu.cs.dingcan.entity.DinnerTable;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.edu.sdjzu.cs.dingcan.entity.Food;
import cn.edu.sdjzu.cs.dingcan.entity.FoodType;
import cn.edu.sdjzu.cs.dingcan.service.FoodService;
import cn.edu.sdjzu.cs.dingcan.service.FoodTypeService;
import cn.edu.sdjzu.cs.dingcan.utils.Condition;
import cn.edu.sdjzu.cs.dingcan.utils.PageBean;

@WebServlet(name="IndexServlet", urlPatterns = "/index")
public class IndexServlet extends HttpServlet {
	/**
	 *
	 */
	private static final long serialVersionUID = -7740457085576705413L;
	private final Logger logger = LoggerFactory.getLogger(IndexServlet.class);
	private static final String TABLE_LIST_ATTR = "tableList";

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = StringUtils.defaultIfBlank(request.getParameter("method"), "default-page");

		try {
			switch (method) {
				case "get-menu":
				case "default-page":
					this.getMenu(request, response);				
					break;
				case "food-detail":
					this.getFoodDetail(request, response);
					break;
			}
		} catch(SQLException e) {
			throw new RuntimeException("执行数据库操作时遇到错误", e);
		}		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if(StringUtils.isBlank(method)) {
			response.sendError(HttpServletResponse.SC_BAD_REQUEST, "method参数未提供或为空");
			return;
		}
		switch (method) {
			case "search-food":
				try {
					this.searchFood(request, response);
				} catch (SQLException throwables) {
					throwables.printStackTrace();
				}
				break;
		}
	}

	public void getMenu(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		FoodTypeService foodTypeService = new FoodTypeService();
		FoodService foodService = new FoodService();

		// 查询菜系信息
		List<FoodType> foodTypes = foodTypeService.findAll();
		request.setAttribute("foodtypes", foodTypes);

		// 获取菜单页面信息
		PageBean<Food> pb = new PageBean<Food>();

		Condition con = new Condition();
		// 获取页面得到的参数
		String foodTypeId = request.getParameter("food-type-id");
		String foodName = request.getParameter("food-name");
		if (foodTypeId != null && !foodTypeId.isEmpty()) {
			con.setFoodTypeId(Long.parseLong(foodTypeId));
			pb.setCondition(con);
		}
		if (foodName != null && !foodName.isEmpty()) {
			con.setFoodName(foodName);
			pb.setCondition(con);
		}

		pb.setPageCount(6);
		String curPage = request.getParameter("current-page");// 获取当前页
		if (curPage == null || curPage.isEmpty()) {
			pb.setCurrentPage(1);
		}
		if (curPage != null && !curPage.isEmpty()) {
			int currentPage = Integer.parseInt(curPage);
			pb.setCurrentPage(currentPage);
		}

		foodService.getAll(pb);

		request.setAttribute("pageBean", pb);

		request.getRequestDispatcher("/app/detail/caidan.jsp").forward(request, response);
	}

	public void searchFood(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException, SQLException {
		FoodService foodService = new FoodService();
		PageBean<Food> pb = new PageBean<Food>();
		Condition condition = new Condition();
		String keyword = request.getParameter("keyword");//设置关键词
		if(StringUtils.isNotBlank(keyword)){
			condition.setFoodName(keyword);
		}
		if(condition != null){
			pb.setCondition(condition);
		}
		
		pb.setCondition(condition);
		foodService.getAll(pb);
		request.setAttribute("pageBean", pb);
		// 跳转
		request.getRequestDispatcher("/app/detail/caidan.jsp").forward(request, response);
	}
	
	public void getFoodDetail(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, NumberFormatException, SQLException {
		FoodService foodService = new FoodService();
		String id = request.getParameter("food-id");//获取食物id
		Food food = foodService.getById(Long.parseLong(id));
		request.setAttribute("food", food);
		request.getRequestDispatcher("/app/detail/caixiangxi.jsp").forward(request, response);
	}
}
